/*
 * @Author: 晟松
 * @Date: 2022-03-06 15:53:14
 * @LastEditors: 晟松
 * @LastEditTime: 2022-03-06 19:22:43
 * @FilePath: \mini-vue3d:\Code\node\koa2\volunteer-service\src\main\web3js\Init.js
 * @Description: 针对数据库里面的数据，将其作为一个交易，存放到区块链中，并存下txhash和blockhash
 */

import sequelize from "@main/db";
import { isNull, isBlank } from "@main/util/ValidateUtil";
import { ParamError, LoginError, NotFoundError } from "@main/common/CommonError";
import { encryptPassword, checkPassword } from "@main/util/PasswordUtil";
import config from "@main/config";
import blacklist from "blacklist";
import whiteListed from "whitelisted";
import enums from "@main/config/enums";
import { toEnumNum, toEnumStr } from "@main/util/EnumsUtil";
import { uploadFile } from "@main/util/UploadUtils";
import { xlsxAnalysis } from "@main/util/XlsxUtils";
import { Op } from "sequelize/dist";

// import {
//     remixCommunity,
//     remixGlobalMessage,
//     remixGroupVol,
//     remixLogin,
//     remixMessage,
//     remixResident,
//     remixSchool,
//     remixStudent,
//     remixUser,
//     remixVol,
// } from "@main/web3js/web3";

import remix from "@main/web3js/web3";

// 获取所有定义的model
const models = sequelize.models;

class Init {
    /**
     * @description:初始化登录表的数据
     * @param {*}
     * @return {*}
     */
    static async initLogin(tableName) {
        let allLogin = await models.login.findAll();
        let len = allLogin.length;
        for (let i = 0; i < len; i++) {
            // 注意是dataValues
            let data = allLogin[i].dataValues;
            console.log("data: ", data);
            let res = await remix.remixLogin(data);
            console.log("res: ", res);
            // 然后进行更新
            await models["login"].update(res, {
                where: {
                    id: data.id,
                },
            });
        }
    }
    /**
     * @description:初始化studentGroupVol表的数据
     * @param {*}
     * @return {*}
     */
     static async initStudentGroupVol() {
        let allLogin = await models['student_groupVol'].findAll();
        let len = allLogin.length;
        for (let i = 0; i < len; i++) {
            // 注意是dataValues
            let data = allLogin[i].dataValues;
            console.log("data: ", data);
            let res = await remix.remixStudentGroupVol(data);
            console.log("res: ", res);
            // 然后进行更新，这里是特殊的表的列的处理
            await models["student_groupVol"].update(res, {
                where: {
                    studentId:data.studentId,
                    groupVolId:data.groupVolId
                },
            });
        }
    }
    /**
     * @description: 通过传入一个tableName，自动读取这个表的数据并存储更新
     * @param {*} tableName 例如 Login Community GroupVol...注意首字母要大写
     * @return {*}
     */
    static async initTable(tableName) {
        // 注意这里首字母需要转一下小写
        let table = tableName[0].toLowerCase() + tableName.slice(1)
        let allTableValues = await models[table].findAll();

        let len = allTableValues.length;
        for (let i = 0; i < len; i++) {
            // 注意是dataValues
            let data = allTableValues[i].dataValues;
            console.log("data: ", data);
            // 使用这种进行多种化的调用
            let res = await remix[`remix${tableName}`](data);
            console.log("res: ", res);
            // 然后进行更新
            await models[table].update(res, {
                where: {
                    id: data.id,
                },
            });
        }
    }
    static async init() {
        let table = ["Community", "GlobalMessage", "GroupVol", "Login", "Message", "Resident", "School", "Student", "User", "Vol"];
        for (let i = 0; i < table.length; i++) {
            await Init.initTable(table[i]);
        }
    }
}

export default Init;
// initLogin()
